package io.renren.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: xiaomi
 * @date: 2022/2/17
 * @description: 正则匹配的相关方法
 */
public class MatcherT {

    public static void main(String[] args) {
        String content = "关乎全人类的生命，全人类的命运人类";
        String regex = "(人类|生人|全人类).{0,3}(生命|命运)";
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(content);
        StringBuilder sb = new StringBuilder();
        int beginIndex = 0;
        while (matcher.find()) {
            //pre
            sb.append(content.substring(beginIndex, matcher.start()));
            //core
            sb.append("[").append(content.substring(matcher.start(), matcher.end())).append("]");
            //post
            beginIndex = matcher.end();
        }
        sb.append(content.substring(beginIndex));
        System.out.println(sb.toString());
    }
}
